// orderPackage/pages/shoppingCart/shoppingCart.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkeAll: false, //商品全选
    list: [],
    totalPrice: 0, //总价
    totalNum: 0, //总数,
    isShowDel: false,
    userScore: 0, //用户积分

  },

  // 商品详情
  toDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
    })

  },

  // 删除
  delHandle() {
    var list = this.data.list,
      arr = list.filter(item => {
        return !item.checked
      });
    wx.showModal({
      title: '提示',
      content: '是否删除所选商品',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          this.setData({
            list: arr
          })
          this.getTotalHandle()
        }
      }
    })
  },
  // 结算
  submitHandle() {
    if (this.data.totalNum == 0) {
      wx.showToast({
        title: '请选择要结算的商品',
        icon: 'none'
      })
      return
    }
    console.log(this.data.list);
    var arr = this.data.list.filter(ele => {
      ele.selectFormat = {
        factoryId: ele.factoryId,
        formatId: ele.formatId,
        formatName: ele.formatName,
        productId: ele.productId,
        selectNum: ele.cartNum,
        totalMoney: Number((ele.productPrice * ele.cartNum).toFixed(2)),
        productPrice: ele.productPrice,
        productScore: ele.productScore,
        postNum:ele.postNum
      }
      return ele.checked
    })
    console.log(arr);


    wx.navigateTo({
      url: '/orderPackage/pages/submitOrder/submitOrder?order=' + JSON.stringify(arr),
    })

    console.log(this.data.list);
    console.log(this.data.totalNum);
    console.log(this.data.totalPrice);
  },

  // 改变商品数量
  onNumChange(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index,
      list = this.data.list;
    if (e.detail > list[index].productNum) {
      list[index].cartNum = list[index].productNum
    } else {
      list[index].cartNum = e.detail
    }

    list[index].cartPrice = list[index].cartNum * list[index].productPrice

    this.setData({
      list
    })
    this.getTotalHandle()
  },


  getTotalHandle() {
    var list = this.data.list,
      totalPrice = 0,
      isShowDel = false,
      checkeAll = this.data.checkeAll,
      totalNum = 0;
    list.forEach(item => {
      if (item.checked) {
        totalNum += Number(item.cartNum)
        totalPrice += Number(item.cartNum) * item.productPrice
      }
    })

    isShowDel = list.some(item => {
      return item.checked
    })
    checkeAll = list.every(item => {
      return item.checked
    })
    this.setData({
      totalNum,
      totalPrice,
      isShowDel,
      checkeAll
    })
  },

  // 单选商品
  onProChange(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index,
      list = this.data.list;
    list[index].checked = !list[index].checked

    this.setData({
      list
    })
    this.getTotalHandle()


  },
  // 全选商品
  changeAllproduct(e) {
    console.log(e);
    var flag = e.detail,
      list = this.data.list;
    list.forEach(ele => {
      ele.checked = flag
    })


    this.setData({
      list,
      checkeAll: flag
    })
    this.getTotalHandle()
  },


  // 获取数据
  getList() {
    var url = app.globalData.url + "miniMain/listCart",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId
      }, res => {
        console.log(res);
        if (res.flag) {

          wx.hideLoading()
          res.data.forEach(ele => {
            ele.checked = false
          })

          that.setData({
            list: res.data
          })
          console.log(that.data.list);
        }
      },
      err => {
        console.log(err);
      })
  },
  toPage() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userScore: app.globalData.userScore,
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})